


# 重写jwt返回结果方法
def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'username':user.username,
        'user_id':user.id
    }

# 重写authenticate实现手机号也能完成登陆的功能
from django.contrib.auth.backends import ModelBackend
import re
from users.models import User
class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            if re.match("1[3-9]\d{9}", username):
                user = User.objects.get(mobile=username)
            else:
                user = User.objects.get(username=username)
        except:
            return None

        if not user is None and user.check_password(password):
            return user




